//
//  MyVC.m
//  MyTest
//
//  Created by zhiyou on 16-4-12.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "MyVC.h"
#import "LoginVC.h"
#import "MySelfCell.h"
#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height
const CGFloat kNavigationBarHeight = 44;
const CGFloat kStatusBarHeight = 20;

@interface MyVC () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) UIView *headerContentView;
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *imgArr;
@end

@implementation MyVC
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = YES;
//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//    //去掉底部线条
//    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _dataArray = @[@[@"购物车",@"我的订单",@"我的钱包",@"我的收藏",@"我的晒物",@"我赞过的商品",@"收货地址"],@[@"客服",@"设置",@"关于版本"]];
    _imgArr = @[@[@"icon0",@"icon_1",@"icon2",@"icon3",@"icon4",@"icon5",@"icon6"],@[@"icon_0",@"icon1",@"icon_2"]];
    
    [self.view addSubview:self.tableView];
    [self headerView];
    self.tableView.tableHeaderView = _headerContentView;
}

#pragma mark -- 初始化TableView
-(UITableView *)tableView{
    if (_tableView == nil) {
        
        CGRect tableView_frame = CGRectMake(0, 0, Width, Height-49);
        _tableView = [[UITableView alloc] initWithFrame:tableView_frame style:UITableViewStyleGrouped];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        [_tableView registerClass:[MySelfCell class] forCellReuseIdentifier:@"MyCell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

#pragma mark -- UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else {
        return [_dataArray[section-1] count];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MySelfCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLabel.text = _dataArray[indexPath.section-1][indexPath.row];
        cell.imageView.image = [UIImage imageNamed:_imgArr[indexPath.section-1][indexPath.row]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 70;
    } else {
        return 50;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        [self goToLogin];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark --创建表头
- (void)headerView
{
    CGRect bounds = CGRectMake(0, 0, Width, Height/3);
    _headerContentView = [[UIView alloc] initWithFrame:bounds];
    _headerContentView.backgroundColor = [UIColor blackColor];
    //背景
    _bgImageView = [[UIImageView alloc] initWithFrame:bounds];
    _bgImageView.image = [UIImage imageNamed:@"backImage.jpg"];
    [_headerContentView addSubview:_bgImageView];
    
    UIImageView *headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"G_icon_normal"]];
    headImageView.layer.cornerRadius = 5;
    headImageView.clipsToBounds = YES;
    [_headerContentView addSubview:headImageView];
    
    [headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headerContentView.mas_centerX);
        make.top.offset(40);
        make.width.height.offset(60);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectZero;
    [button setTitle:@"登录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"userLoginOut"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(goToLogin) forControlEvents:UIControlEventTouchUpInside];
    [_headerContentView addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headerContentView.mas_centerX);
        make.top.equalTo(headImageView.mas_bottom);
        make.width.offset(Width/4);
        make.height.offset(35);
    }];
}
#pragma mark -- 拉伸效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offset_Y = scrollView.contentOffset.y;
    
    if (offset_Y < 0) {
        float scale = Width*3/Height;
        CGFloat height = Height/3-offset_Y;
        CGFloat width = Width-offset_Y*scale;
        _bgImageView.frame = CGRectMake(offset_Y/2, offset_Y, width, height);
    }
}

- (void)goToLogin
{
    LoginVC *vc = [[LoginVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
